package cn.plugins.generator.plugin

import cn.plugins.generator.utils.GenerateCodeUtil
import org.apache.maven.plugin.AbstractMojo
import org.apache.maven.plugins.annotations.Mojo
import org.apache.maven.plugins.annotations.Parameter
// import org.gradle.internal.impldep.org.apache.maven.plugin.AbstractMojo

/**
 * @Description: maven代码生成插件
 * @Author: jy
 * @CreateTime: 2021/11/1
 */
@Mojo(name = "gcp"/*, defaultPhase = LifecyclePhase.COMPILE*/)
open class MavenPlugin : AbstractMojo() {

    @Parameter(required = true)
    lateinit var dbType: String                 // 数据库类型
    @Parameter(required = true)
    lateinit var url: String                    // 数据库连接地址
    @Parameter(required = true)
    lateinit var port: String                   // 端口
    @Parameter(required = true)
    lateinit var database: String               // 数据库名称
    @Parameter(required = true)
    lateinit var userName: String               // 用户名
    @Parameter(required = true)
    lateinit var password: String               // 密码
    @Parameter(required = true)
    lateinit var frameworkType: String          // 框架类型(mybatis或mybatisplus)
    @Parameter(required = true)
    lateinit var tablePrefix: String            // 表前缀
    @Parameter(required = true)
    lateinit var tableName: String              // 表名称，多个使用英文逗号隔开
    @Parameter(required = true)
    lateinit var projectPath: String            // 项目绝对路径
    @Parameter(required = true)
    lateinit var packageName: String            // 包名称
    @Parameter(required = true)
    lateinit var controller: String             // 控制器文件夹名称
    @Parameter(required = true)
    lateinit var service: String                // 业务层文件夹名称
    @Parameter(required = true)
    lateinit var mapper: String                 // dao层文件夹名称
    @Parameter(required = true)
    lateinit var entity: String                 // 实体文件夹名称
    @Parameter(required = true)
    lateinit var xml: String                    // dao层对应xml文件夹名称
    @Parameter(required = true)
    lateinit var languageType: String           // 语言类型，目前只支持java和kotlin
    @Parameter(required = true)
    lateinit var author: String                 // 作者
    @Parameter(required = true)
    lateinit var company: String                // 所属公司
    @Parameter(required = true)
    lateinit var apiPrefix: String              // 接口前缀
    @Parameter(required = true)
    var isRestfulStyle: Boolean = false         // 是否controller使用resfful风格
    @Parameter(required = true)
    var enableGenerateDoc: Boolean = true       // 是否允许生成doc
    @Parameter(required = true)
    var enableGenVuePage: Boolean = false       // 是否生成vue页面
    //lateinit var isServerH2: Int              // 是否为独立服务h2数据库

    override fun execute() {
        this.generateAction()
    }

    fun generateAction() {
        if (dbType.isBlank()) {
            println("database type can't be null or empty")
            return
        }
        if (dbType != "mysql" || dbType != "postgresql") {
            println("database type only surport mysql or postgresql")
            return
        }
        if (url.isBlank()) {
            println("url can't be null or empty")
            return
        }
        if (port.isBlank()) {
            println("port can't be null or empty")
            return
        }
        if (database.isBlank()) {
            println("databaseName can't be null or empty")
            return
        }
        if (userName.isBlank()) {
            println("userName can't be null or empty")
            return
        }
        if (password.isBlank()) {
            println("password can't be null or empty")
            return
        }
        if (frameworkType.isBlank()) {
            println("password can't be null or empty")
            return
        }
        if (tablePrefix.isBlank()) {
            println("frameworkType can't be null or empty")
            return
        }
        if (tableName.isBlank()) {
            println("tableName can't be null or empty")
            return
        }
        if (projectPath.isBlank()) {
            println("projectPath can't be null or empty")
            return
        }
        if (packageName.isBlank()) {
            println("packageName can't be null or empty")
            return
        }
        if (controller.isBlank()) {
            println("controller can't be null or empty")
            return
        }
        if (service.isBlank()) {
            println("service can't be null or empty")
            return
        }
        if (mapper.isBlank()) {
            println("mapper can't be null or empty")
            return
        }
        if (entity.isBlank()) {
            println("entity can't be null or empty")
            return
        }
        if (xml.isBlank()) {
            println("xml can't be null or empty")
            return
        }
        if (languageType.isBlank()) {
            println("languageType can't be null or empty")
            return
        }
        if (author.isBlank()) {
            println("author can't be null or empty")
            return
        }
        if (company.isBlank()) {
            println("company can't be null or empty")
            return
        }
        if (languageType != "java" && languageType != "kotlin") {
            println("only surpport generate java or kotlin code")
            return
        }
        if (apiPrefix.isBlank()) {
            apiPrefix = "api"
            println("default api prefix is:/api")
        }

        url = when(dbType) {
            "mysql" -> "jdbc:mysql://${url}:${port}/${database}?useUnicode=true&serverTimezone=Asia/Shanghai&useSSL=false&characterEncoding=UTF-8&allowPublicKeyRetrieval=true&autoReconnect=true&failOverReadOnly=false&tinyInt1isBit=false"
            "postgresql" -> "jdbc:postgresql://${url}:${port}/${database}"
            // "h2" -> "jdbc:h2:"
            else -> ""
        }

        try {
            println("========================== start generate code ==========================")
            println("========================== author: iwiteks-jy  ==========================")
            println("========================== version: 1.0.6      ==========================")
            GenerateCodeUtil.generateCode(
                dbType,
                url,
                database,
                userName,
                password,
                frameworkType,
                tablePrefix,
                tableName,
                projectPath,
                packageName,
                controller,
                service,
                mapper,
                entity,
                xml,
                languageType,
                author,
                company,
                apiPrefix,
                isRestfulStyle,
                enableGenerateDoc,
                enableGenVuePage
            )
        } catch (ex: Exception) {
            println(ex)
        } finally {
            println("========================== end generate code ==========================")
        }
    }
}